import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestAction {
    public static void main(String[] args) {
        //按下按钮的时候触发事件
        Frame frame = new Frame();
        Button button = new Button();
        //因为addActionListener()需要一个ActionListener，所以我们构造一个ActionListener
        MyActionListerner myActionListerner = new MyActionListerner();
        button.addActionListener(myActionListerner);
        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);
        frame.setVisible(true);
    }
    //关闭窗口的事件
    public static void windowClose (Frame frame){
        frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
}
class MyActionListerner implements ActionListener{
    //重写接口实现方法
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("you pushed it");
    }
}